package com.com.ruanmeng.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.View;

import com.com.ruanmeng.utils.ToolUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 17/09/21.
 */

public class LineView extends View {
    private final static String X_KEY = "Xpos";
    private final static String Y_KEY = "Ypos";
    public float YPoint = (float)(198 * getResources().getDisplayMetrics().density + 0.5f);//Lineview在布局中的dp高度转换为px
//    public static float YPoint = (float) 555;//Lineview在布局中的dp高度转换为px
//    public static float YPoint = (float) 400.5;
    private float XScale = (float)(16 * getResources().getDisplayMetrics().density + 0.5f);  //刻度长度
//    private float XScale = (float) 48.0;  //刻度长度
    private float YScale = (float) 2.5;
    private List<Map<String, Integer>> mListPoint = new ArrayList<Map<String, Integer>>();

    public Paint mPaint = new Paint();

    public LineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public LineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public LineView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(2);

        for (int index = 0; index < mListPoint.size(); index++) {
            if (index > 0) {
                canvas.drawLine(mListPoint.get(index - 1).get(X_KEY) * XScale, YPoint - (mListPoint.get(index - 1).get(Y_KEY)),
                        mListPoint.get(index).get(X_KEY) * XScale, YPoint - (mListPoint.get(index).get(Y_KEY)), mPaint);
//                canvas.drawLine(mListPoint.get(index-1).get(X_KEY), mListPoint.get(index-1).get(Y_KEY),
//                        mListPoint.get(index).get(X_KEY), mListPoint.get(index).get(Y_KEY), mPaint);
                canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
            }
        }
    }

    /**
     * @param curX which x position you want to draw.
     * @param curY which y position you want to draw.
     */
    public void setLinePoint(int curX, int curY) {
        Map<String, Integer> temp = new HashMap<String, Integer>();
        temp.put(X_KEY, curX);
        temp.put(Y_KEY, curY);
        mListPoint.add(temp);
        invalidate();
    }
    public void clear(){
        mListPoint.clear();
        invalidate();
    }
    public void setcolor(Integer color){
        mPaint.setColor(color);
    }
    public void show() {
        setWillNotDraw(false);
        invalidate();
    }
}
